<template>
    <div class="mod-prod">
        <avue-crud ref="crud"
                   :data="dataList"
                   :option="tableOption"
                   :page="page"
                   :permission="permission"
                   :table-loading="dataListLoading"
                   @search-change="searchChange"
                   @selection-change="selectionChange"
                   @refresh-change="refreshChange"
                   @on-load="getDataList"
                   @row-del="deleteHandle"
        >
            <template slot="menuLeft">
                <el-button
                    icon="el-icon-plus"
                    size="small"
                    type="primary"
                    @click.stop="addOrUpdateHandle()">新增
                </el-button>
                
                <!--                 <el-button v-if="isAuth('shop:pickAddr:delete')"-->
                <!--                           :disabled="dataListSelections.length <= 0"-->
                <!--                           size="small"-->
                <!--                           type="danger"-->
                <!--                           @click="deleteHandle()">批量删除-->
                <!--                </el-button>-->
            </template>
            <template slot="isAllcust"
                      slot-scope="scope">
                <el-tag v-if="scope.row.isAllcust === '1'"
                        size="small">是
                </el-tag>
                <el-tag v-else-if="scope.row.isAllcust === '0'"
                        size="small">否
                </el-tag>
            </template>
            <template slot="menu"
                      slot-scope="scope">
                <el-button
                    v-if="scope.row.isEnable === '1'"
                    icon="el-icon-message-solid"
                    size="small"
                    type="primary"
                    @click="autoTask(scope.row)">生成任务
                </el-button>
                <el-button
                    icon="el-icon-edit"
                    size="small"
                    type="primary"
                    @click="addOrUpdateHandle(scope.row.id)">修改
                </el-button>
                <el-button
                    icon="el-icon-delete"
                    size="small"
                    type="danger"
                    @click="deleteHandle(scope.row.id)">删除
                </el-button>
            
            </template>
        </avue-crud>
        <add-or-update v-if="addOrUpdateVisible"
                       ref="addOrUpdate"
                       @refreshDataList="refreshChange"></add-or-update>
    </div>
</template>

<script>
import {tableOption} from '@/crud/customer/custCareRuleList'
import AddOrUpdate from '@/views/modules/customer/custcarerule-add-update.vue'

export default {
    components: {AddOrUpdate},
    data () {
        return {
            dataForm: {
                prodName: ''
            },
            permission: {
                delBtn: this.isAuth('prod:prod:delete')
            },
            dataList: [],
            page: {
                total: 0, // 总页数
                currentPage: 1, // 当前页数
                pageSize: 1000, // 每页显示多少条
                pageSizes: [1000]
            },
            addOrUpdateVisible: false,
            dataListSelections: [],
            dataListLoading: false,
            tableOption: tableOption,
            resourcesUrl: process.env.VUE_APP_RESOURCES_URL
        }
    },
    methods: {
        // 获取数据列表
        getDataList (page, params, done) {
            this.dataListLoading = true
            this.$http({
                url: this.$http.adornUrl('/crm/base/cusRule/page'),
                method: 'get',
                params: this.$http.adornParams(
                    Object.assign(
                        {
                            current: page == null ? this.page.currentPage : page.currentPage,
                            size: page == null ? this.page.pageSize : page.pageSize
                        },
                        params
                    )
                )
            }).then(({data}) => {
                this.dataList = data.obj.records
                this.page.total = data.obj.total
                this.dataListLoading = false
                if (done) {
                    done()
                }
            })
        },
        // 新增 / 修改
        addOrUpdateHandle (id) {
            console.log('🚀 ~ id', id)
            this.addOrUpdateVisible = true
            this.$nextTick(() => {
                this.$refs.addOrUpdate.init(id)
            })
        },
        autoTask(row){
            this.$http({
                url: this.$http.adornUrl('/crm/base/cusRule/genCareTask'),
                method: 'post',
                data: this.$http.adornData({
                    ...row
                })
            }).then(({ data }) => {
                this.$message({
                    message: '生成任务成功！请到【客户关怀任务】查看',
                    type: 'success',
                    duration: 1500,
                    onClose: () => {
                        this.visible = false
                        this.$emit('refreshDataList')
                    }
                })
            })
        },
        // 删除和批量删除
        deleteHandle (id) {
            console.log('id:',id);
            this.$confirm(`确定进行删除操作?`, '友情提醒', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            })
                .then(() => {
                    this.$http({
                        url: this.$http.adornUrl(`/crm/base/cusRule/`+ id),
                        method: 'delete',
                        data: this.$http.adornData({id: id}, false)
                    }).then(({data}) => {
                        this.getDataList(this.page)
                        this.$message({
                            message: '操作成功',
                            type: 'success',
                            duration: 1500,
                            onClose: () => {
                            }
                        })
                    })
                })
                .catch(() => {
                })
        },
        // 条件查询
        searchChange (params, done) {
            this.getDataList(this.page, params, done)
        },
        // 多选变化
        selectionChange (val) {
            this.dataListSelections = val
        },
        // 获取选中的商品Id列表
        getSeleProdIds () {
            return this.dataListSelections.map(item => {
                return item.prodId
            })
        },
        /**
         * 刷新回调
         */
        refreshChange () {
            this.getDataList(this.page)
        },
    }
}
</script>
